﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Info;

namespace RedBit.UserVoice.Helpers
{
    //Code borrowed from NickHarris.net
    //TODO: ask Nick how he'd like to be thanked when we OpenSource this
    public static class UserIdHelper
    {
        private static readonly int ANIDLength = 32;
        private static readonly int ANIDOffset = 2;


        public static string GenerateUserId()
        {
            string result = string.Empty;
            object anid;
            if (UserExtendedProperties.TryGetValue("ANID", out anid))
            {
                if (anid != null && anid.ToString().Length >= (ANIDLength + ANIDOffset))
                {
                    result = anid.ToString().Substring(ANIDOffset, ANIDLength);
                }
            }
            
            if(result == string.Empty )
            {
                object uniqueId;
                if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
                    result = Convert.ToBase64String((uniqueId as byte[]));
                else
                    result = Guid.NewGuid().ToString();
 
            }


            return result; 
        }

    }
}
